module MSFileValidator

  VALID_MS_LINE = /\A\s* \d+[,.]?\d* \s+ \d+[,.]?\d* \s*\z/x

  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def validates_ms_file_structure(*args)
      validate :ms_file_structure, *args
    end
  end

  def ms_file_structure
    unless ms_file && ms_file.each_line.all? { |line| line.match(VALID_MS_LINE) }
      errors.add(:ms_file, "must be a whitespace separated list of mass-intensity pairs")
    end
  end
end
